package com.mo.lib.utils.time

import com.dylanc.longan.mainThreadHandler
import java.util.*

/**
 * @ author：mo
 * @ data：:
 * @ 功能：定时执行、定时器
 */

fun actionTimer(delay: Long, period: Long,block:()->Unit):Timer{
val timer= Timer()
    timer.schedule(object : TimerTask() {
        override fun run() {
            mainThreadHandler.post { block.invoke() }
        }
    }, delay, period)
    return timer
}
 class TimerHelper {
    private var mTimer: Timer? = null

    /**delay从现在起过delay毫秒以后开始，每隔period毫秒执行一次 */
    fun start(delay: Long, period: Long,block:()->Unit) {
        stop()
        mTimer = Timer()
        mTimer?.schedule(object : TimerTask() {
            override fun run() {
                mainThreadHandler.post {
                    block.invoke()
                }
            }
        }, delay, period)
    }

    fun stop() {
        mTimer?.let {
            it.cancel()
            mTimer = null
        }
    }

    fun isTimerIsRuning(): Boolean {
        return null != mTimer
    }

}